package com.qcby.types.context;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 当前登录用户的临时保存容器
 * @author cong.zhen
 * @date 2024/04/16
 */
public class LoginUserHolder {
    private static final ThreadLocal<CaLoginUser> LONGIN_USER_HOLDER = new ThreadLocal<>();

    /**
     * set holder中内容
     *
     * @author cong.zhen
     * @date 2021/3/23 17:41
     */
    public static void set(CaLoginUser abstractLoginUser) {
        LONGIN_USER_HOLDER.set(abstractLoginUser);
    }

    /**
     * 获取holder中的值
     *
     * @author cong.zhen
     * @date 2021/3/23 17:41
     */
    public static CaLoginUser get() {
        return LONGIN_USER_HOLDER.get();
    }

    /**
     * 获得账户名称
     *
     * @return {@link String}
     */
    public static String getUserName() {
        CaLoginUser caLoginUser = LONGIN_USER_HOLDER.get();
        if (ObjectUtil.isNull(caLoginUser)) {
            return StrUtil.EMPTY;
        }
        return caLoginUser.getUserName();
    }

    /**
     * 得到真实姓名
     *
     * @return {@link String}
     */
    public static String getNickName() {
        CaLoginUser caLoginUser = LONGIN_USER_HOLDER.get();
        if (ObjectUtil.isNull(caLoginUser)) {
            return StrUtil.EMPTY;
        }
        return caLoginUser.getNickName();
    }

    /**
     * 删除保存的用户
     *
     * @author fengshuonan
     * @date 2021/3/23 17:42
     */
    public static void remove() {
        LONGIN_USER_HOLDER.remove();
    }
}
